import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/media_asset_data_entity.dart';
import 'package:lcapp/data/zixun_data_entity.dart';
import 'package:lcapp/model/text/expandable_text.dart';
import 'package:lcapp/page/home/home_share_page.dart';
import 'package:lcapp/tools/date_util.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/asr_html.dart';

class ZixunDetailItem extends StatefulWidget {
  final ZixunDataEntity entity;
  const ZixunDetailItem({Key? key, required this.entity}) : super(key: key);

  @override
  _ZixunDetailItemState createState() => _ZixunDetailItemState();
}

class _ZixunDetailItemState extends State<ZixunDetailItem> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        if(widget.entity.layout==3){
          Navigator.of(context).pushNamed('ZixunDetailPage',arguments: widget.entity.id);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        padding: EdgeInsets.only(bottom: 30.w),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Visibility(child: Container(
                  child: Row(
                    children: [
                      Expanded(child: Container(
                        height: 160.w,
                        padding: EdgeInsets.only(right: 30.w),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(bottom: 20.w),
                              child: Text(widget.entity.title??'',maxLines: 2,overflow: TextOverflow.ellipsis,style: TextStyle(
                                  color: Color(0xff444444),
                                  fontWeight: FontWeight.w500,
                                  fontSize: 32.sp
                              ),),
                            ),
                            Container(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(child: Text(widget.entity.origin??'',maxLines: 1,style: TextStyle(
                                      color:Color(0xff444444),
                                      fontSize: 28.sp,
                                      fontWeight: FontWeight.w500
                                  ),)),
                                  Container(
                                    width: 140.w,
                                    height: 36.w,
                                    decoration: BoxDecoration(
                                        color: Color(0x1fFFB48E),
                                        borderRadius: BorderRadius.circular(6.w),
                                        border: Border.all(color: Color(0xffFFCEB6))
                                    ),

                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        Text('查看更多 ',style: TextStyle(
                                          color:Color(0xffEA6B2B),
                                          fontSize: 20.sp,
                                        ),),
                                        Icon(Icons.arrow_forward_ios,size: 20.w,color: Color(0xffEA6B2B),)
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),

                          ],
                        ),
                      ),),
                      ClipRRect(
                        borderRadius: BorderRadius.circular(10.w),
                        child: ASRCacheImage(
                          image: widget.entity.images!,
                          fit: BoxFit.cover,
                          width: 200.w,
                          height: 160.w,
                        ),
                      )
                    ],
                  ),
                ),visible: widget.entity.layout==3,),
                Visibility(child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: Text(widget.entity.title!,style: TextStyle(
                            color: widget.entity.flag == 1? Color(0xffd7431f): Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 36.sp
                        ),),
                      ),
                      Container(
                        child:  ExpandableText(
                          text:widget.entity.content??'',
                          style:TextStyle(
                              color:  widget.entity.flag == 1? Color(0xffd7431f):Color(0xff444444),
                              fontSize: 32.sp,
                              height: 1.6
                          ),
                          maxLines: 6,
                          textAlign: TextAlign.left,
                          btnOpen: Container(
                            padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 6.w),
                            decoration: BoxDecoration(
                                color: Color(0xffFFCEB6),
                                borderRadius: BorderRadius.circular(6.w)
                            ),
                            child: Text('展开',style:TextStyle(
                                color:Color(0xffEA6B2B),
                                fontSize: 24.sp
                            )),),
                          // btnClose: Container(
                          //   padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 6.w),
                          //   decoration: BoxDecoration(
                          //       color: Color(0xffFFCEB6),
                          //       borderRadius: BorderRadius.circular(6.w)
                          //   ),
                          //   child: Text('收起',style:TextStyle(
                          //     color: Color(0xffEA6B2B),
                          //     fontSize: 24.sp
                          // )),),
                        ),
                      ),

                    ],
                  ),
                ),visible: widget.entity.layout==1,),
                Visibility(child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: Text(widget.entity.title!,style: TextStyle(
                            color: widget.entity.flag == 1? Color(0xffd7431f): Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 36.sp
                        ),),
                      ),
                      Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: ExpandableText(
                          text:widget.entity.content??'',
                          style:TextStyle(
                              color:  widget.entity.flag == 1? Color(0xffd7431f):Color(0xff444444),
                              fontSize: 32.sp,
                              height: 1.6
                          ),
                          maxLines: 5,
                          textAlign: TextAlign.left,
                          btnOpen: Container(
                            padding: EdgeInsets.symmetric(horizontal: 20.w,vertical: 6.w),
                            decoration: BoxDecoration(
                                color: Color(0xffFFCEB6),
                                borderRadius: BorderRadius.circular(6.w)
                            ),
                            child: Text('展开',style:TextStyle(
                                color:Color(0xffEA6B2B),
                                fontSize: 24.sp
                            )),),
                          // btnClose: Container(
                          //   color: Colors.green,
                          //   child: Text('收起',style:TextStyle(
                          //     color: Color(0xff444444),
                          //     fontSize: 24.sp
                          // )),),
                        ),
                      ),
                      GestureDetector(
                        onTap: (){
                          MediaAssetDataEntity entity = MediaAssetDataEntity();
                          entity.type=1;
                          entity.uri = widget.entity.images;
                          Navigator.of(context).pushNamed('MediaViewer', arguments: {
                            'assets': [entity],
                            'currentIndex': 0
                          });
                        },
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10.w),
                          child: ASRCacheImage(
                            image: widget.entity.images!,
                            fit: BoxFit.cover,
                            width: 200.w,
                            height: 160.w,
                          ),
                        ),
                      )
                    ],
                  ),
                ),visible: widget.entity.layout==2,),
              ],
            ))
          ],
        ),
      ),
    );
  }

  String getDate(String date){
    if(date==null||date.isEmpty){
      return '';
    }
    DateTime currentTime = DateTime.now();
    int year = currentTime.year;
    List times = date.split('-');
    if(times[0] == year.toString()){
      return '${times[1]}-${times[2]}';
    }
    return date;
  }

  String getTime(String date,{int? type}){

    if(date==null||date.isEmpty){
      return '';
    }
    List<String> times = date.split(' ');
    String day = DateUtil.getTiemDate(DateTime.now().millisecondsSinceEpoch);
    if(times.isNotEmpty&&times.length==2){
      if(type==2){
        return times[1];
      }
      if(type==1&&day != times[0]){
        List days = times[0].split('-');
        return '${days[1]}-${days[2]}  ';
      }
    }
    return '';
  }
}
